package com.datastructure.test.kdis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

public class KDis {

    public static void main(String[] args) {
        ArrayList<Integer> nums = new ArrayList<>();
        nums.add(3);
        nums.add(1);
        nums.add(4);
        nums.add(6);
        nums.add(4);
        KDis kDis = new KDis();
        int k_dis = kDis.k_dis(nums, 3);
        System.out.println(k_dis);
    }

    public int k_dis (ArrayList<Integer> nums, int k) {
        // write code here
        int num = 0;
        ArrayList<Integer> collect = (ArrayList<Integer>)nums.stream().distinct().map(i -> i - k).collect(Collectors.toList());
        for (Integer j : collect) {
            if (nums.contains(j)) {
                num++;
            }
        }
        return num;
    }

    public int k_dis1 (ArrayList<Integer> nums, int k) {
        // write code here
        List<Integer> collect = nums.stream().distinct().collect(Collectors.toList());
        HashMap<Integer, Integer> integerHashMap = new HashMap<>();
        for (Integer integer : collect) {
            integerHashMap.put(integer - k, integer);
        }
        int count = 0;
        for (Integer integer : collect) {
            if (integerHashMap.containsKey(integer)) ++count;
        }

        return count;
    }
}
